﻿using ExitGames.Logging;
using Photon.SocketServer;
using PhotonHostRuntimeInterfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TaiDouCommon.Model;
using TaiDouPhotonServer.Handlers;

namespace TaiDouPhotonServer
{
    //用来跟客户端进行通信
    class ClientPeer:PeerBase
    {
        #region constants and fields
        private static readonly ILogger log = ExitGames.Logging.LogManager.GetCurrentClassLogger();

        //登录用户，需缓存
        public User loginUser{get;set;}//当前登录的帐号
        public Role loginRole { get; set; }//当前登录的角色

        #endregion


        #region Method

        public ClientPeer(IRpcProtocol protocol, IPhotonPeer peer) : base(protocol, peer) { 

        }

        //与客户端连接失败
        protected override void OnDisconnect(PhotonHostRuntimeInterfaces.DisconnectReason reasonCode, string reasonDetail)
        {
            //throw new NotImplementedException();
        }

        //客户端发起的请求
        protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
        {
            HandlerBase handler = TaiDouApplication.Instance.GetHandlerByCode(operationRequest.OperationCode);
            if (handler != null)
            {
                //协议处理完毕，则给客户端返回响应数据
                OperationResponse response = handler.OnHandleMessage(operationRequest, this);
                SendOperationResponse(response, sendParameters);
            }
            else {
                log.Warn("can not find handler for code:" + operationRequest.OperationCode);
            }
        }

        #endregion
    }
}
